package com.example.speeddating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.speeddating.constants.TaoHuaConstant;
import com.example.speeddating.entity.Like;
import com.example.speeddating.mapper.LikeMapper;
import com.example.speeddating.service.LikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.speeddating.service.TaohuaDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {

    @Autowired
    private TaohuaDetailService taohuaDetailService;

    @Autowired
    private LikeMapper likeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLike(Like like) {
        taohuaDetailService.insertTaohuaDetail(TaoHuaConstant.Type_2,like.getUserId());
        return likeMapper.insertLike(like);
    }

//    @Override
//    public int removeById(Long id) {
//        return likeMapper.deleteLike(id);
//    }

    @Override
    public int deleteLike(Object articleId, Object commentId, Long curUserId) {

        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        //articleId不为null的时候才查询
        if (articleId != null) {
            queryWrapper.eq("article_id", articleId);
        }
        //commentId不为null的时候才查询
        if (commentId != null) {
            queryWrapper.eq("comment_id", commentId);
        }
        queryWrapper.eq("user_id", curUserId);
        int delete = likeMapper.delete(queryWrapper);
        return delete;
//        Long ar = null;
//        return likeMapper.deleteLike(articleId, commentId, curUserId);
    }
}
